package zqservlet;

import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import zqdb.O_INFO;
import zqtools.DBUtil;

import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "com2Servlet", urlPatterns = "/com2")
public class com2Servlet extends HttpServlet {
    static {
        ArtemisConfig.host = "10.7.60.45:443";// 代理API网关nginx服务器ip端口
        ArtemisConfig.appKey = "25415289";// 秘钥appkey
        ArtemisConfig.appSecret = "XE2ajSWkBS0FFcuwQeuE";// 秘钥appSecret
    }
    private static final String ARTEMIS_PATH = "/artemis";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        O_INFO of = new O_INFO();

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String s = parameterNames.nextElement();
            switch (s) {
                case "URL":
                    of.setURL(request.getParameter(s));
                    break;
                case "CMD":
                    of.setCMD(request.getParameter(s));
                    break;
                case "VALUE01":
                    of.setVALUE01(request.getParameter(s));
                    break;
                case "VALUE02":
                    of.setVALUE02(request.getParameter(s));
                    break;
                case "VALUE03":
                    of.setVALUE03(request.getParameter(s));
                    break;
                case "VALUE04":
                    of.setVALUE04(request.getParameter(s));
                    break;
                case "VALUE05":
                    of.setVALUE05(request.getParameter(s));
                    break;
                case "VALUE06":
                    of.setVALUE06(request.getParameter(s));
                    break;
                case "VALUE07":
                    of.setVALUE07(request.getParameter(s));
                    break;
                case "VALUE08":
                    of.setVALUE08(request.getParameter(s));
                    break;
                case "VALUE09":
                    of.setVALUE09(request.getParameter(s));
                    break;
                case "VALUE10":
                    of.setVALUE10(request.getParameter(s));
                    break;
                case "VALUE11":
                    of.setVALUE11(request.getParameter(s));
                    break;
                case "VALUE12":
                    of.setVALUE12(request.getParameter(s));
                    break;
                case "VALUE13":
                    of.setVALUE13(request.getParameter(s));
                    break;
                case "VALUE14":
                    of.setVALUE14(request.getParameter(s));
                    break;
                case "VALUE15":
                    of.setVALUE15(request.getParameter(s));
                    break;
                case "VALUE16":
                    of.setVALUE16(request.getParameter(s));
                    break;
                case "VALUE17":
                    of.setVALUE17(request.getParameter(s));
                    break;
                case "VALUE18":
                    of.setVALUE18(request.getParameter(s));
                    break;
                case "VALUE19":
                    of.setVALUE19(request.getParameter(s));
                    break;
                case "VALUE20":
                    of.setVALUE20(request.getParameter(s));
                    break;
                case "PIC1":
                    of.setPIC1(request.getParameter(s));
                    break;
                case "PIC2":
                    of.setPIC2(request.getParameter(s));
                    break;
                case "PIC3":
                    of.setPIC3(request.getParameter(s));
                    break;
                case "PIC4":
                    of.setPIC4(request.getParameter(s));
                    break;
                case "PIC5":
                    of.setPIC5(request.getParameter(s));
                    break;
                case "PIC6":
                    of.setPIC6(request.getParameter(s));
                    break;
                case "PIC7":
                    of.setPIC7(request.getParameter(s));
                    break;
                case "PIC8":
                    of.setPIC8(request.getParameter(s));
                    break;
                case "PIC9":
                    of.setPIC9(request.getParameter(s));
                    break;
            }
        }

        JSONObject j = DBUtil.f_query2(of);
        if ("BF".equals(of.getCMD())) {
            if ("ZZ00".equals(j.getJSONArray("db1").getJSONObject(0).getString("VALUE01"))) {
                getpath4(j.getJSONArray("db1").getJSONObject(0).getString("VALUE02"));
            }
        }
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print(j);
        j.clear();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    //--特殊业务逻辑
    //--依据personid删除海康中的人员信息
    final static String VechicleDataApi4 = ARTEMIS_PATH + "/api/resource/v1/person/batch/delete";
    static Map<String, String> path4 = new HashMap<String, String>(2) {
        {
            put("https://", VechicleDataApi4);
        }
    };

    public static void getpath4(String as_personid) {
        com.alibaba.fastjson.JSONObject jsonBody = new com.alibaba.fastjson.JSONObject();
        String[] personid = new String[]{as_personid};

        jsonBody.put("personIds", personid);
        String body = jsonBody.toJSONString();
        HttpResponse result = ArtemisHttpUtil.doPostStringImgArtemis(path4, body, null, null, "application/json", null);
        try {
            HttpResponse resp = result;
            if (200 == resp.getStatusLine().getStatusCode()) {
                HttpEntity entity = resp.getEntity();
                String str = EntityUtils.toString(entity);
                com.alibaba.fastjson.JSONObject obj = com.alibaba.fastjson.JSONObject.parseObject(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
